package cn.desk.user.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.desk.commons.service.impl.BaseServiceImpl;
import cn.desk.commons.util.QueryCondition;
import cn.desk.user.entity.Menu;
import cn.desk.user.entity.User;
import cn.desk.user.service.IMenuService;

public class MenuServiceImpl<T extends Menu> extends BaseServiceImpl<T>
		implements IMenuService<T> {
	private static final Logger logger = LoggerFactory
			.getLogger(IMenuService.class);

	@Override
	public T loadByUserAndParent(User u, T t) {
		logger.debug("loadByUserAndParent start");
		try {
			T temp;
			QueryCondition condition = new QueryCondition();
			condition.andEq("user", u);
			if (t.getId() == null || "null".equals(t.getId())) {
				condition.andIsNull("parent");
				temp = baseDao.loadByCondition(Menu.class, condition);
			} else {
				temp = baseDao.load(Menu.class, t.getId());
				// condition.andEq("parent", temp);
			}
			if (temp.getParent() == null) {
				temp.setId(null);
			}
			// T temp=baseDao.loadByCondition(Menu.class, condition);
			return temp;
		} finally {
			logger.debug("loadByUserAndParent end");
		}
	}
}
